package com.ssy.lingxi.contract.controller;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.model.vo.sign.request.AutoSignRequest;
import com.ssy.lingxi.contract.model.vo.sign.request.ContractSignatureRequest;
import com.ssy.lingxi.contract.model.vo.sign.request.OrderSettleSignDetailRequest;
import com.ssy.lingxi.contract.model.vo.sign.response.ContractSignatureUrlResponse;
import com.ssy.lingxi.contract.model.vo.sign.response.ContractUploadResponse;
import com.ssy.lingxi.contract.model.vo.sign.response.SignContractCreateResponse;
import com.ssy.lingxi.contract.model.vo.sign.response.SignatorySignDetailResponse;
import com.ssy.lingxi.contract.service.IContractSignatureService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @menu 合同能力 - 合同签署
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/27
 */
@RestController
@RequestMapping("/contract/signature")
public class ContractSignatureController extends BaseController {

    @Resource
    private IContractSignatureService contractSignatureService;

    /**
     * 新增合同 - 生成电子合同
     * @param request 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/contractCreate")
    public Wrapper<SignContractCreateResponse> contractCreate(@RequestBody @Validated ContractSignatureRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractSignatureService.contractCreate(request, sysUser);
    }

    /**
     * 新增合同 - 上传合同文件
     * @param request 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/contractUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Wrapper<ContractUploadResponse> contractUpload(@RequestPart("file") MultipartFile file, HttpServletRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractSignatureService.contractUpload(file, sysUser);
    }

    /**
     * 签订合同 - 获取签署地址
     * @param request 请求参数
     * @return 返回结果
     */
    @GetMapping(value = "/getHandSignatureUrl")
    public Wrapper<ContractSignatureUrlResponse> getHandSignatureUrl(@Validated AutoSignRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractSignatureService.getHandSignatureUrl(request, sysUser);
    }

    /**
     * 签订合同 - 查询签署人签署状态(轮询)
     * @param request 请求参数
     * @return 返回结果
     */
    @GetMapping(value = "/getSignatorySignStatus")
    public Wrapper<SignatorySignDetailResponse> getSignatorySignStatus(@Validated OrderSettleSignDetailRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractSignatureService.getSignatorySignStatus(request, sysUser);
    }
}
